package com.example.wan_android.viewmodel

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.wan_android.http.bean.NavItemData
import com.example.wan_android.http.bean.SystemItemData
import com.example.wan_android.http.code.NavRepository
import com.example.wan_android.http.code.SystemRepository
import com.example.wan_android.ui.activity.KnowledgeBodyActivity
import com.example.wan_android.ui.activity.WebActivity
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SystemViewModel : BaseViewModel() {
    private val repository = SystemRepository()
    private val repository2 = NavRepository()
    private val _systemItemLiveData = MutableLiveData<Result<List<SystemItemData>>>()
    val systemItemLiveData = _systemItemLiveData
    private val _navLiveData = MutableLiveData<Result<List<NavItemData>>>()
    val navLiveData = _navLiveData
    private val _isSuccess = MutableLiveData<Boolean>(false)
    val isSuccess = _isSuccess

    fun getNavItems() {
        _isSuccess.value = false
        viewModelScope.launch {
            _navLiveData.value = repository2.fetchNavItems()
            delay(500)
            _isSuccess.value = true
        }
    }

    fun getSystems() {
        _isSuccess.value = false
        viewModelScope.launch {
            _systemItemLiveData.value = repository.fetchSystems()
            delay(500)
            _isSuccess.value = true
        }
    }

    fun openKnowledgeBody(index: Int, id: Int, title: String) {
        startActivity(Intent().apply {
            putExtra(KnowledgeBodyViewModel.index, index)
            putExtra(KnowledgeBodyViewModel.id, id)
            putExtra(KnowledgeBodyViewModel.title, title)
        }, targetClass = KnowledgeBodyActivity::class.java)
    }

    fun openWeb(url: String) {
        startActivity(Intent().apply {
            putExtra(WebViewModel.url, url)
        }, targetClass = WebActivity::class.java)
    }
}